package com.yixun.qfbao.utils;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.yixun.qfbao.hander.GetCRDetailHander;
import com.yixun.qfbao.model.PartnerInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/**
 * 
 * <p>webUtil</p>
 */
public class WebUtil {
    private static Logger logger= LoggerFactory.getLogger(WebUtil.class);

    private static final String LOGIN_USER = "qfb_xf_loginPartner";

    public static PartnerInfo getPartnerInfo(HttpServletRequest request) {
        return (PartnerInfo) getSession(request).getAttribute(LOGIN_USER);
    }

    public static void removePartnerInfo(HttpServletRequest request) {
        getSession(request).removeAttribute(LOGIN_USER);
    }

    public static void addPartnerInfo(HttpServletRequest request, PartnerInfo partnerInfo) {
        getSession(request).setAttribute(LOGIN_USER, partnerInfo);
    }

    /**
     * 
     *<p>判断是否为ajax请求<p>
     * @Title isAjax 
     * @param request
     * @return boolean    返回类型 
     */
    public static boolean isAjax(HttpServletRequest request) {
        boolean isAjax = false;
        String ajaxRequest = request.getHeader("x-Requested-With");
        if (ajaxRequest != null && ajaxRequest.equals("XMLHttpRequest")) {
            isAjax = true;
        }
        return isAjax;
    }
    
    public static HttpSession getSession(HttpServletRequest request) {
        return request.getSession();
    }

    public static void sendErrorMessage(HttpServletRequest request,HttpServletResponse response){
        ServletOutputStream out = null;
        try{
            if(request.getHeader("x-requested-with") != null && request.getHeader("x-requested-with").equalsIgnoreCase("XMLHttpRequest")){//ajax请求
                response.setCharacterEncoding("UTF-8");
                response.setContentType("application/json; charset=utf-8");
                out = response.getOutputStream();
                out.write(("{\"result\":\"-1\",\"errorMsg\":\"没有相应权限！\"}").getBytes("UTF-8"));
            }
        }catch (IOException e){
            logger.error("sendErrorMessage error: {}", e);
        }finally {
            if(out != null){
                try{
                    out.flush();
                    out.close();
                }catch (IOException e){
                    logger.error("sendErrorMessage error: {}", e);
                }
            }
        }
    }

    public static void sendNoLoginMessage(HttpServletRequest request,HttpServletResponse response){
        ServletOutputStream out = null;
        try{
            if(request.getHeader("x-requested-with") != null && request.getHeader("x-requested-with").equalsIgnoreCase("XMLHttpRequest")){//ajax请求
                response.setCharacterEncoding("UTF-8");
                response.setContentType("application/json; charset=utf-8");
                out = response.getOutputStream();
                out.write(("{\"result\":\"99\",\"errorMsg\":\"该用户未登陆！\"}").getBytes("UTF-8"));
            }
        }catch (IOException e){
            logger.error("sendNoLoginMessage error: {}", e);
        }finally {
            if(out != null){
                try{
                    out.flush();
                    out.close();
                }catch (IOException e){
                    logger.error("sendNoLoginMessage error: {}", e);
                }
            }
        }
    }

}
